package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateCustomProcessBase
//@description: 创建CustomProcessBase记录
//@param: customProcessBase model.CustomProcessBase
//@return: err error

func CreateCustomProcessBase(customProcessBase model.CustomProcessBase) (err error) {
	err = global.GVA_DB.Create(&customProcessBase).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBase
//@description: 删除CustomProcessBase记录
//@param: customProcessBase model.CustomProcessBase
//@return: err error

func DeleteCustomProcessBase(customProcessBase model.CustomProcessBase) (err error) {
	err = global.GVA_DB.Delete(customProcessBase).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBaseByIds
//@description: 批量删除CustomProcessBase记录
//@param: ids request.IdsReq
//@return: err error

func DeleteCustomProcessBaseByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.CustomProcessBase{}, "id in ?", ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCustomProcessBase
//@description: 更新CustomProcessBase记录
//@param: customProcessBase *model.CustomProcessBase
//@return: err error

func UpdateCustomProcessBase(customProcessBase *model.CustomProcessBase) (err error) {
	err = global.GVA_DB.Save(customProcessBase).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBase
//@description: 根据id获取CustomProcessBase记录
//@param: id uint
//@return: err error, customProcessBase model.CustomProcessBase

func GetCustomProcessBase(id uint) (err error, customProcessBase model.CustomProcessBase) {
	err = global.GVA_DB.Where("id = ?", id).First(&customProcessBase).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBaseInfoList
//@description: 分页获取CustomProcessBase记录
//@param: info request.CustomProcessBaseSearch
//@return: err error, list interface{}, total int64

func GetCustomProcessBaseInfoList(info request.CustomProcessBaseSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.CustomProcessBase{})
	var customProcessBases []model.CustomProcessBase
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.Name != "" {
		db = db.Where("`name` LIKE ?", "%"+info.Name+"%")
	}
	if info.FromUrl != "" {
		db = db.Where("`from_url` LIKE ?", "%"+info.FromUrl+"%")
	}
	if info.ToUrl != "" {
		db = db.Where("`to_url` LIKE ?", "%"+info.ToUrl+"%")
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&customProcessBases).Error
	return err, customProcessBases, total
}
